<?php
namespace api\common\model;
use think\Db;
class User extends \think\Model 
{
    protected $autoWriteTimestamp = true;
    protected $table = 'users';
    // token,会员编号,昵称,性别,头像,状态,vip状态,用户地址,手机号,vip开始时间,vip结束时间,用户姓名
    protected $login = array('token','vipCard','userName','userSex','userPic','status','userStatus','address','userPhone','startTime','endTime','trueName');

    /**
     * 用户登录
     */
    public function login($openid,$unionid,$userName,$userSex,$userPic){
        $res;
        //先根据opendid和unionid查询用户
        $queue_sel = $this->selUserByou($openid);
        $date = getNowDate();
        if($queue_sel){//存在用户就直接返回
            // 修改token
            $data['loginTime'] = $date;
            $data['token'] = GenerateToken($openid);
            $update = Db::name($this->table)->where('openId',$openid)->update($data);
            if($update){
                $sel = $this->selUserByou($openid);
                if($sel){
                    $res = $sel;
                }
            }
        }else{//不存在就先添加再返回
            $data['openId'] = $openid;
            $data['unionId'] = $unionid;
            $data['userName'] = $userName;
            $data['userSex'] = $userSex;
            $data['userPic'] = $userPic;
            // 设置默认数据
            $data['userStatus'] = 1;
            $data['status'] = 1;
            $data['addtime'] = $date;
            $data['updateTime'] = $date;
            $data['updateTime'] = $date;
            $data['loginTime'] = $date;
            $data['token'] = GenerateToken(1);
            $insert = Db::name($this->table)->insert($data);
            if($insert){
                $queue_j_sel = $this->selUserByou($openid);
                if($queue_j_sel){
                    $res = $queue_j_sel;
                }
            }
        }
        return $res;
    }

    /**
     * 根据opendid
     */
    public function selUserByou($openid){
        $queue_sel = Db::name($this->table)->field($this->login)->where('openId',$openid)->find();
        return $queue_sel;
    }

    /**
     * 修改资料
     * 根据token
     */
    public function updateInfo($token,$name,$phone,$address){
        $date = date('Y-m-d H:i:s');
        $data['updateTime'] = $date;
        $data['trueName'] = $name;
        $data['userPhone'] = $phone;
        $data['address'] = $address;
        $res = Db::name($this->table)->where('token', $token)->update($data);
        return $res;
    }

    /**
     * 修改用户照片信息
     */
    public function updPhoto($userId,$photo){
        $data['userPhoto'] = $photo;
        $data['updateTime'] = getNowDate();
        $res = Db::name($this->table)->where('userId', $userId)->update($data);
        return $res;
    }

    /**
     * 获取用户信息,根据token
     */
    public function getInfo($token){
        $res = Db::name($this->table)->field($this->login)->where('token',$token)->find();
        return $res;
    }

    /**
     * 获取用户所有信息,根据token
     */
    public function getAllInfo($token){
        $res = Db::name($this->table)->where('token',$token)->find();
        return $res;
    }


    /**
     * 根据用户id获取信息
     */
    public function getInfoById($id){
        $res = Db::name($this->table)->find($id);
        return $res;
    }

    /**
     * 根据token获取用户id
     */
    public function getId($token){
        $res = Db::name($this->table)->field('userId')->where('token',$token)->find();
        return $res;
    }

    /**
     * 根据token获取用户openId
     */
    public function getopenId($token){
        $res = Db::name($this->table)->field('openId')->where('token',$token)->find();
        return $res;
    }

    /**
     * 修改用户信息,根据id
     */
    public function updateUserInfo($data){
        $date = date('Y-m-d H:i:s');
        $data['updateTime'] = $date;
        $res = Db::name($this->table)->where('userId',$data['userId'])->update($data);
        return $res;
    }
    /**
     * 获取当前用户的最高编号,返回新用户的编号
     */
    public function selNumber(){
        $queue = Db::query('select MAX(userNumber) as number from mj_users');
        $returNumber = '0001';
        if((int)$queue[0]['number']){
            $returNumber = (int)$queue[0]['number']+1; 
        }
        return setuserNumber($returNumber);
    }
}
